import {
  Request,Login
} from "../../utils/request.js";
const app = getApp();
const res = {
  result: [
    [{
      cidx: [0, 15],
      fullname: "北京市",
      id: "110000",
      location: {
        lat: 39.90469,
        lng: 116.40717 
      },
      name: "北京",
      pinyin: ["bei", "jing"]
    }, {
      cidx: [16, 31],
      fullname: "天津市",
      id: "120000",
      location: {lat: 39.0851, lng: 117.19937},
      name: "天津",
      pinyin: ["tian", "jin"]
    }, {
      cidx: [32, 42],
      fullname: "河北省",
      id: "130000",
      location: {lat: 38.03599, lng: 114.46979},
      name: "河北",
      pinyin: ["he", "bei"],
    }, {
      cidx: [43, 53],
      fullname: "山西省",
      id: "140000",
      location: {lat: 37.87343, lng: 112.56272},
      name: "山西",
      pinyin:  ["shan", "xi"],
    }]
  ]
}
Page({

  /**
   * 页面的初始数据
   */
  data: {
    statusBarHeight: getApp().globalData.statusBarHeight,
    userid: getApp().globalData.userid,
    open:1, //切换聊天 关注
    listss: ["A","B","C","D","E","F","G","H","I","J","K","L","N","M","O","P","Q","R","S","T","U","V","W","S","Y","Z"],
  },
  onReachBottom: function(param) {
    // Do something when page reach bottom.
    console.log('触底'+param);
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getCitys();
    this.url =   wx.getStorageSync("config");
     /**
     * 获取用户 关注列表
     */
    // 暂时注释 不需要关注列表
    // new Request('friend/Get_Follow/GetList').request(
    //   () => {
    //     return {
    //       openid: app.globalData.openid,
    //       userid: app.globalData.userid,
    //     }
    //   },
    //   (res) => {
    //     if(res.data.data == '' || res.data.data == null){
    //       new Request().showToast('当前没有关注好友');
    //       return;
    //     }
    //     res.data.data.map((e)=>{
    //       e.attenimage = this.url.service.host + e.attenimage;
    //     });
    //     this.setData({
    //       attenList: res.data.data, //关注列表
    //     });
    //   }
    // );
    //获取用户最新聊天 最近一条消息
    new Request('Zyconcern/wechatlist').request(
      () => {
        return {
          openid: app.globalData.openid,
          userid: app.globalData.userid
        }
      },
      (res) => {
        if(res.data.data != null){   //游客身份
          this.setData({
            wechatList: res.data.data, //关注列表
            userid: app.globalData.userid,
          });
  
          let info = new Array();
          res.data.data.forEach(function(elem){
            let obj = {id:elem.id, uid:elem.uid };
            this.push(obj);
          },info);

          /**
           * 获取用户 最近新一条聊天内容
           */
          // this.intervalId = setInterval(() => {
          // new Request('Zyconcern/wechatlist').request(
          //   () => {
          //     return {
          //       openid: app.globalData.openid,
          //       userid: app.globalData.userid
          //     }
          //   },
          //   (res) => {
          //     this.setData({
          //       wechatList: res.data.data, //关注列表
          //     });
          //   }
          // );
          // }, 3000);
  
          // JSON.stringify();
          // console.log(info);
        }else{
          //游客
          //用户未注册 提示跳转注册页面
          new Login().LoginDown();
          return;
        }
      }
    );    
  },
  /**
   * 
   * @param {_bindscroll} param 
   */
  _bindscroll:function(param){
    console.log(param);
  },
  /**
   * 切换聊天 关注
   */
  qietap:function(param){
    if(param.currentTarget.id == 1){
      this.data.open = 1;
    }else if(param.currentTarget.id == 2){
      this.data.open = 2;
    }else if(param.currentTarget.id == 3){
      this.data.open = 3;
    }
    this.setData({
      open: this.data.open,
    });
  },
  getCitys() {
    const _this = this
    const cities = res.result[0]
        // 按拼音排序
        cities.sort((c1, c2) => {
          let pinyin1 = c1.pinyin.join('')
          let pinyin2 = c2.pinyin.join('')
          return pinyin1.localeCompare(pinyin2)
        })
        // 添加首字母
        const map = new Map()
        for (const city of cities) {
          const alpha = city.pinyin[0].charAt(0).toUpperCase()
          if (!map.has(alpha)) map.set(alpha, [])
          map.get(alpha).push({ name: city.fullname })
        }
    
        const keys = []
        for (const key of map.keys()) {
          keys.push(key)
        }
        keys.sort()
    
        const list = []
        for (const key of keys) {
          list.push({
            alpha: key,
            subItems: map.get(key)
          })
        }

        _this.setData({list});
  },
  onChoose(e) {
    console.log('onChoose', e)
  },
  /**
   * 跳转到聊天界面
   */
  gotoContact:function(e){
    console.log(e) //uid点击进入聊天对象的界面
    wx.navigateTo({
      url: '/pages/zhaon_xiaoxi/zhaon_xiaoxi_contact/zhaon_xiaoxi_contact?uid=' + e.currentTarget.dataset.uid + '&uimg=' + this.url.service.host + e.currentTarget.dataset.uimg + '&name=' + e.currentTarget.dataset.username,
      success:()=>{
        new Request('Zyconcern/updateXiaoContent').request(
          () => {
            return {
              openid: app.globalData.openid,
              userid: app.globalData.userid,
              uid: e.currentTarget.dataset.uid
            }
          },
          (res) => {
            this.setData({
              wechatList: res.data.data, //关注列表
            });
          }
        );
      }
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    console.log('页面显示了')
    //游客
    if(app.globalData.loginCode == -1){
      //用户未注册 提示跳转注册页面
      new Login().LoginDown();
      return;
    }
    /**
     * 获取用户 最近新一条聊天内容
     */
    new Request('Zyconcern/wechatlist').request(
      () => {
        return {
          openid: app.globalData.openid,
          userid: app.globalData.userid
        }
      },
      (res) => {
        this.setData({
          wechatList: res.data.data, //关注列表
        });
      }
    );
    /**
     * 获取用户 最近新一条聊天内容
     */
    if(!this.intervalId){
      this.intervalId = setInterval(() => {
        new Request('Zyconcern/wechatlist').request(
          () => {
            return {
              openid: app.globalData.openid,
              userid: app.globalData.userid
            }
          },
          (res) => {
            this.setData({
              wechatList: res.data.data, //关注列表
            });
          }
        );
      }, 3000);
    }
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    console.log('页面隐藏了'+this.intervalId);
    clearInterval(this.intervalId);
    this.intervalId = '';
    console.log('清除了'+this.intervalId);
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    console.log('页面卸载了');
    console.log('页面隐藏了content'+this.intervalId);
    clearInterval(this.intervalId);
    this.intervalId = '';
    console.log('清除了content'+this.intervalId);
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})